////////////////////////////////////////////////////////////////////////////
//
//  Crytek Engine Source File.
//  Copyright (C), Crytek Studios, 2008.
// -------------------------------------------------------------------------
//  File name:   TVEventsDialog.h
//  Version:     v1.00
//  Created:     3/4/2008 by Kevin.
//  Compilers:   Visual Studio.NET
//  Description: 
// -------------------------------------------------------------------------
//  History:
//
////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "TVEventsDialog.h"
#include "StringDlg.h"


// CTVEventsDialog dialog

IMPLEMENT_DYNAMIC(CTVEventsDialog, CDialog)

CTVEventsDialog::CTVEventsDialog(IAnimSequence *pTrack, CWnd* pParent /*=NULL*/)
	: CDialog(CTVEventsDialog::IDD, pParent),
	m_pSequence(pTrack)
{

}

CTVEventsDialog::~CTVEventsDialog()
{
}

void CTVEventsDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EVENTS_LIST, m_List);
}


BEGIN_MESSAGE_MAP(CTVEventsDialog, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_ADDEVENT, &CTVEventsDialog::OnBnClickedButtonAddEvent)
	ON_BN_CLICKED(IDC_BUTTON_REMOVEEVENT, &CTVEventsDialog::OnBnClickedButtonRemoveEvent)
	ON_BN_CLICKED(IDC_BUTTON_RENAMEEVENT, &CTVEventsDialog::OnBnClickedButtonRenameEvent)
	ON_BN_CLICKED(IDC_BUTTON_UPEVENT, &CTVEventsDialog::OnBnClickedButtonUpEvent)
	ON_BN_CLICKED(IDC_BUTTON_DOWNEVENT, &CTVEventsDialog::OnBnClickedButtonDownEvent)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_EVENTS_LIST, OnListItemChanged)
END_MESSAGE_MAP()


// CTVEventsDialog message handlers

void CTVEventsDialog::OnBnClickedButtonAddEvent()
{
	CStringDlg dlg( _T("Track Event Name") );
	if (dlg.DoModal() == IDOK && !dlg.GetString().IsEmpty())
	{
		CString add = dlg.GetString();

		// Make sure it doesn't already exist
		LVFINDINFO find;
		find.flags = LVFI_STRING|LVFI_WRAP;
		find.psz = add;
		if (-1 == m_List.FindItem(&find))
		{
			for (int k=0; k<m_List.GetItemCount(); ++k)
				m_List.SetItemState(k, 0, LVIS_SELECTED);
			m_List.InsertItem(m_List.GetItemCount(), add);
			m_List.SetItemState(m_List.GetItemCount()-1, LVIS_SELECTED, LVIS_SELECTED);
			m_pSequence->AddTrackEvent(add);
		}
	}
	m_List.SetFocus();
}

void CTVEventsDialog::OnBnClickedButtonRemoveEvent()
{
	POSITION pos = m_List.GetFirstSelectedItemPosition();
	while (pos)
	{
		int index = m_List.GetNextSelectedItem(pos);
		if (MessageBox("This removal might cause some link breakages in Flow Graph.\nStill continue?", "Remove Event", MB_YESNO|MB_ICONWARNING) == IDYES)
		{
			CString eventName = m_List.GetItemText(index, 0);
			m_List.DeleteItem(index);
			m_pSequence->RemoveTrackEvent(eventName);
			pos = m_List.GetFirstSelectedItemPosition();
		}
	}
	m_List.SetFocus();
}

void CTVEventsDialog::OnBnClickedButtonRenameEvent()
{
	POSITION pos = m_List.GetFirstSelectedItemPosition();
	if (pos)
	{
		int index = m_List.GetNextSelectedItem(pos);
		CStringDlg dlg( _T("Track Event Name") );
		if (dlg.DoModal() == IDOK && !dlg.GetString().IsEmpty())
		{
			CString oldName = m_List.GetItemText(index, 0);
			CString newName = dlg.GetString();
			if (oldName != newName)
			{
				// Make sure it doesn't already exist
				LVFINDINFO find;
				find.flags = LVFI_STRING|LVFI_WRAP;
				find.psz = newName;
				if (-1 == m_List.FindItem(&find))
				{
					m_List.SetItemText(index, 0, newName);
					m_pSequence->RenameTrackEvent(oldName, newName);
				}
			}
		}
	}
	m_List.SetFocus();
}

void CTVEventsDialog::OnBnClickedButtonUpEvent()
{
	POSITION pos = m_List.GetFirstSelectedItemPosition();
	if (pos)
	{
		int index = m_List.GetNextSelectedItem(pos);
		if (index > 0)
		{
			CString up = m_List.GetItemText(index-1, 0);
			CString down = m_List.GetItemText(index, 0);
			m_List.SetItemText(index-1, 0, down);
			m_List.SetItemText(index, 0, up);
			m_List.SetItemState(index-1, LVIS_SELECTED, LVIS_SELECTED);
			m_List.SetItemState(index, 0, LVIS_SELECTED);
			m_pSequence->MoveUpTrackEvent(down);
		}
	}
	m_List.SetFocus();
}

void CTVEventsDialog::OnBnClickedButtonDownEvent()
{
	POSITION pos = m_List.GetFirstSelectedItemPosition();
	if (pos)
	{
		int index = m_List.GetNextSelectedItem(pos);
		if (index < m_List.GetItemCount() - 1)
		{
			CString up = m_List.GetItemText(index, 0);
			CString down = m_List.GetItemText(index+1, 0);
			m_List.SetItemText(index, 0, down);
			m_List.SetItemText(index+1, 0, up);
			m_List.SetItemState(index, 0, LVIS_SELECTED);
			m_List.SetItemState(index+1, LVIS_SELECTED, LVIS_SELECTED);
			m_pSequence->MoveDownTrackEvent(up);
		}
	}
	m_List.SetFocus();
}

BOOL CTVEventsDialog::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Push existing items into list
	assert(m_pSequence);
	const int iCount = m_pSequence->GetTrackEventsCount();
	for (int i = 0; i < iCount; ++i)
	{
		m_List.InsertItem(i, m_pSequence->GetTrackEvent(i));
	}

	UpdateButtons();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CTVEventsDialog::OnListItemChanged(NMHDR* pNMHDR, LRESULT* pResult)
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	// if( pNMListView->uNewState & LVIS_SELECTED )
	{
		// int selectedItem = pNMListView->iItem;
		UpdateButtons();
	}

	*pResult = 0;
}
void CTVEventsDialog::UpdateButtons()
{
	BOOL bRemove=FALSE, bRename=FALSE, bUp=FALSE, bDown=FALSE;

	UINT nSelected = m_List.GetSelectedCount();
	if (nSelected > 1)
	{
		bRemove = TRUE;
		bRename = FALSE;
	}
	else if (nSelected > 0)
	{
		bRemove = bRename = TRUE;

		POSITION pos = m_List.GetFirstSelectedItemPosition();
		int index = m_List.GetNextSelectedItem(pos);
		if (index > 0)
			bUp = TRUE;
		if (index < m_List.GetItemCount() - 1)
			bDown = TRUE;
	}

	GetDlgItem(IDC_BUTTON_REMOVEEVENT)->EnableWindow(bRemove);
	GetDlgItem(IDC_BUTTON_RENAMEEVENT)->EnableWindow(bRename);
	GetDlgItem(IDC_BUTTON_UPEVENT)->EnableWindow(bUp);
	GetDlgItem(IDC_BUTTON_DOWNEVENT)->EnableWindow(bDown);
}